package com.gdth.bank.presenter;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import com.gdth.bank.model.CjkhLxfsModel;
import com.gdth.bank.view.CjkhLxfsView;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjLxfs;
import com.gdth.database.bean.TAppBfrjThjl;

public class CjkhLxfsPresenter {
	private CjkhLxfsView mCjkhLxfsView;
	private CjkhLxfsModel mCjkhLxfsModel;
	
	public CjkhLxfsPresenter(CjkhLxfsView mCjkhLxfsView) {
		this.mCjkhLxfsView = mCjkhLxfsView;
		this.mCjkhLxfsModel = new CjkhLxfsModel();
	}
	
	/**
	 * 获取所有联系方式
	 * @param jcuuid
	 */
	public void getLxfsByUuid(String jcuuid){
		mCjkhLxfsModel.getLxfsByUuid(jcuuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppBfrjLxfs>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<TAppBfrjLxfs> arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.getAllBrjl(arg0);
			}
		});
	}
	
	public void save(TAppBfrjLxfs mTAppBfrjLxfs) {
		mCjkhLxfsView.showProgressDialog("正在保存...");
		final TAppBfrjLxfs tAppBfrjLxfs = mTAppBfrjLxfs;
		mCjkhLxfsModel.saveCjkhLxfs(mTAppBfrjLxfs)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Subscriber<Long>(){

				@Override
				public void onCompleted() {
					// TODO Auto-generated method stub
					mCjkhLxfsView.hideProgressDialog();
					mCjkhLxfsView.showMessage("保存成功");
				}

				@Override
				public void onError(Throwable arg0) {
					// TODO Auto-generated method stub
					mCjkhLxfsView.hideProgressDialog();
					mCjkhLxfsView.showMessage(arg0.getMessage());
				}

				@Override
				public void onNext(Long arg0) {
					// TODO Auto-generated method stub
					tAppBfrjLxfs.setId(arg0);
					mCjkhLxfsView.saveSuccess(tAppBfrjLxfs);
				}
			});
	}
	
	public void saveThjl(TAppBfrjThjl mTAppBfrjThjl, String sfz) {
		mCjkhLxfsView.showProgressDialog("正在保存...");
		
		mCjkhLxfsModel.saveCjkhThjl(mTAppBfrjThjl, sfz)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<Long>(){
			
			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mCjkhLxfsView.hideProgressDialog();
				mCjkhLxfsView.showMessage("保存成功");
			}
			
			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.showMessage(arg0.getMessage());
			}
			
			@Override
			public void onNext(Long arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.saveSuccessThjl(arg0);
			}
		});
	}
	
	/**
	 * 获取所有通话记录
	 * @param jcuuid
	 */
	public void getThjlByUuid(String jcuuid){
		mCjkhLxfsModel.getThjlByUuid(jcuuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppBfrjThjl>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.showMessage("获取失败");
			}

			@Override
			public void onNext(List<TAppBfrjThjl> arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.getAllThjl(arg0);
			}
		});
	}
	
	/**
	 * 获取所有家庭成员
	 * @param jcuuid
	 */
	public void getJtcyByUuid(String jcuuid){
		mCjkhLxfsModel.getJtcyByUuid(jcuuid)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TAppBfrjJtcyb>>(){
			
			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.showMessage("获取失败");
			}
			
			@Override
			public void onNext(List<TAppBfrjJtcyb> arg0) {
				// TODO Auto-generated method stub
				mCjkhLxfsView.getAllJtcy(arg0);
			}
		});
	}
}
